﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.EntityClient;

using Microsoft.Data.Extensions;
using YhmOA.Model;

namespace YhmOA.EFDAL
{
    public partial class Entities : ObjectContext
    {
        #region Constructors

        /// <summary>
        /// Initializes a new Entities object using the connection string found in the 'Entities' section of the application configuration file.
        /// </summary>
        public Entities()
            : base("name=Entities", "Entities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new Entities object.
        /// </summary>
        public Entities(string connectionString)
            : base(connectionString, "Entities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new Entities object.
        /// </summary>
        public Entities(EntityConnection connection)
            : base(connection, "Entities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new Entities object.
        /// </summary>
        /// <param name="type"></param>
        public Entities(Type type)
            : base(System.Configuration.ConfigurationManager.ConnectionStrings["Entities"].ToString().Replace("[edmx]", string.Format("{0}.{1}.{0}_{1}", type.Name.Split('_')[0], type.Name.Split('_')[1])))
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new Entities object.
        /// </summary>
        /// <param name="oldString"></param>
        /// <param name="newStr"></param>
        public Entities(string oldStr, string newStr)
            : base(System.Configuration.ConfigurationManager.ConnectionStrings["Entities"].ToString().Replace(String.IsNullOrEmpty(oldStr) ? "[edmx]" : oldStr, newStr))
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
        #endregion

        #region Partial Methods

        partial void OnContextCreated();

        #endregion

        #region Integrated_Car ObjectSet Properties

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_AccidentRecord> Integrated_Car_AccidentRecord
        {
            get
            {
                if ((_Integrated_Car_AccidentRecord == null))
                {
                    _Integrated_Car_AccidentRecord = base.CreateObjectSet<Integrated_Car_AccidentRecord>("Integrated_Car_AccidentRecord");
                }
                return _Integrated_Car_AccidentRecord;
            }
        }
        private ObjectSet<Integrated_Car_AccidentRecord> _Integrated_Car_AccidentRecord;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_ApplyCar> Integrated_Car_ApplyCar
        {
            get
            {
                if ((_Integrated_Car_ApplyCar == null))
                {
                    _Integrated_Car_ApplyCar = base.CreateObjectSet<Integrated_Car_ApplyCar>("Integrated_Car_ApplyCar");
                }
                return _Integrated_Car_ApplyCar;
            }
        }
        private ObjectSet<Integrated_Car_ApplyCar> _Integrated_Car_ApplyCar;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_Car> Integrated_Car_Car
        {
            get
            {
                if ((_Integrated_Car_Car == null))
                {
                    _Integrated_Car_Car = base.CreateObjectSet<Integrated_Car_Car>("Integrated_Car_Car");
                }
                return _Integrated_Car_Car;
            }
        }
        private ObjectSet<Integrated_Car_Car> _Integrated_Car_Car;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_CostRecord> Integrated_Car_CostRecord
        {
            get
            {
                if ((_Integrated_Car_CostRecord == null))
                {
                    _Integrated_Car_CostRecord = base.CreateObjectSet<Integrated_Car_CostRecord>("Integrated_Car_CostRecord");
                }
                return _Integrated_Car_CostRecord;
            }
        }
        private ObjectSet<Integrated_Car_CostRecord> _Integrated_Car_CostRecord;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_Dic> Integrated_Car_Dic
        {
            get
            {
                if ((_Integrated_Car_Dic == null))
                {
                    _Integrated_Car_Dic = base.CreateObjectSet<Integrated_Car_Dic>("Integrated_Car_Dic");
                }
                return _Integrated_Car_Dic;
            }
        }
        private ObjectSet<Integrated_Car_Dic> _Integrated_Car_Dic;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_Driver> Integrated_Car_Driver
        {
            get
            {
                if ((_Integrated_Car_Driver == null))
                {
                    _Integrated_Car_Driver = base.CreateObjectSet<Integrated_Car_Driver>("Integrated_Car_Driver");
                }
                return _Integrated_Car_Driver;
            }
        }
        private ObjectSet<Integrated_Car_Driver> _Integrated_Car_Driver;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_InsuranceRecord> Integrated_Car_InsuranceRecord
        {
            get
            {
                if ((_Integrated_Car_InsuranceRecord == null))
                {
                    _Integrated_Car_InsuranceRecord = base.CreateObjectSet<Integrated_Car_InsuranceRecord>("Integrated_Car_InsuranceRecord");
                }
                return _Integrated_Car_InsuranceRecord;
            }
        }
        private ObjectSet<Integrated_Car_InsuranceRecord> _Integrated_Car_InsuranceRecord;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_Maintain> Integrated_Car_Maintain
        {
            get
            {
                if ((_Integrated_Car_Maintain == null))
                {
                    _Integrated_Car_Maintain = base.CreateObjectSet<Integrated_Car_Maintain>("Integrated_Car_Maintain");
                }
                return _Integrated_Car_Maintain;
            }
        }
        private ObjectSet<Integrated_Car_Maintain> _Integrated_Car_Maintain;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_OilCard> Integrated_Car_OilCard
        {
            get
            {
                if ((_Integrated_Car_OilCard == null))
                {
                    _Integrated_Car_OilCard = base.CreateObjectSet<Integrated_Car_OilCard>("Integrated_Car_OilCard");
                }
                return _Integrated_Car_OilCard;
            }
        }
        private ObjectSet<Integrated_Car_OilCard> _Integrated_Car_OilCard;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_OilCardRecord> Integrated_Car_OilCardRecord
        {
            get
            {
                if ((_Integrated_Car_OilCardRecord == null))
                {
                    _Integrated_Car_OilCardRecord = base.CreateObjectSet<Integrated_Car_OilCardRecord>("Integrated_Car_OilCardRecord");
                }
                return _Integrated_Car_OilCardRecord;
            }
        }
        private ObjectSet<Integrated_Car_OilCardRecord> _Integrated_Car_OilCardRecord;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_OilFillRecord> Integrated_Car_OilFillRecord
        {
            get
            {
                if ((_Integrated_Car_OilFillRecord == null))
                {
                    _Integrated_Car_OilFillRecord = base.CreateObjectSet<Integrated_Car_OilFillRecord>("Integrated_Car_OilFillRecord");
                }
                return _Integrated_Car_OilFillRecord;
            }
        }
        private ObjectSet<Integrated_Car_OilFillRecord> _Integrated_Car_OilFillRecord;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_RepairHourRecord> Integrated_Car_RepairHourRecord
        {
            get
            {
                if ((_Integrated_Car_RepairHourRecord == null))
                {
                    _Integrated_Car_RepairHourRecord = base.CreateObjectSet<Integrated_Car_RepairHourRecord>("Integrated_Car_RepairHourRecord");
                }
                return _Integrated_Car_RepairHourRecord;
            }
        }
        private ObjectSet<Integrated_Car_RepairHourRecord> _Integrated_Car_RepairHourRecord;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_RepairPartsRecord> Integrated_Car_RepairPartsRecord
        {
            get
            {
                if ((_Integrated_Car_RepairPartsRecord == null))
                {
                    _Integrated_Car_RepairPartsRecord = base.CreateObjectSet<Integrated_Car_RepairPartsRecord>("Integrated_Car_RepairPartsRecord");
                }
                return _Integrated_Car_RepairPartsRecord;
            }
        }
        private ObjectSet<Integrated_Car_RepairPartsRecord> _Integrated_Car_RepairPartsRecord;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_RepairRecord> Integrated_Car_RepairRecord
        {
            get
            {
                if ((_Integrated_Car_RepairRecord == null))
                {
                    _Integrated_Car_RepairRecord = base.CreateObjectSet<Integrated_Car_RepairRecord>("Integrated_Car_RepairRecord");
                }
                return _Integrated_Car_RepairRecord;
            }
        }
        private ObjectSet<Integrated_Car_RepairRecord> _Integrated_Car_RepairRecord;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_ViolatedRecord> Integrated_Car_ViolatedRecord
        {
            get
            {
                if ((_Integrated_Car_ViolatedRecord == null))
                {
                    _Integrated_Car_ViolatedRecord = base.CreateObjectSet<Integrated_Car_ViolatedRecord>("Integrated_Car_ViolatedRecord");
                }
                return _Integrated_Car_ViolatedRecord;
            }
        }
        private ObjectSet<Integrated_Car_ViolatedRecord> _Integrated_Car_ViolatedRecord;

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Integrated_Car_YearCheckRecord> Integrated_Car_YearCheckRecord
        {
            get
            {
                if ((_Integrated_Car_YearCheckRecord == null))
                {
                    _Integrated_Car_YearCheckRecord = base.CreateObjectSet<Integrated_Car_YearCheckRecord>("Integrated_Car_YearCheckRecord");
                }
                return _Integrated_Car_YearCheckRecord;
            }
        }
        private ObjectSet<Integrated_Car_YearCheckRecord> _Integrated_Car_YearCheckRecord;

        #endregion

        #region users Properties

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<users> users
        {
            get
            {
                if ((_users == null))
                {
                    _users = base.CreateObjectSet<users>("users");
                }
                return _users;
            }
        }
        private ObjectSet<users> _users;

        #endregion

        #region Function Imports
        /// <summary>
        /// 通用查询
        /// </summary>
        /// <param name="tableName">要查询的表</param>
        /// <param name="whereStr">查询条件</param>
        /// <param name="searchColumn">要查询的列</param>
        /// <param name="searchStr">搜索关键字</param>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="pageSize">每页的大小</param>
        /// <param name="orderColumn">排序的列</param>
        /// <param name="order">排序的方向</param>
        /// <param name="count">总记录数</param>
        /// <returns>查询结果</returns>
        public List<T> SearchTable<T>(
            String tableName,
            String whereStr,
            String searchColumn,
            String searchStr,
            Int32 pageIndex,
            Int32 pageSize,
            String orderColumn,
            String order,
            out Int32 count)
        { 
            List<T> list = new List<T>();
            count = 0;
            Materializer<T> listMaterializer = new Materializer<T>();
            Materializer<countClass> countMaterializer = new Materializer<countClass>();

            System.Data.SqlClient.SqlParameter[] sqlparams = new System.Data.SqlClient.SqlParameter[]{
                new System.Data.SqlClient.SqlParameter { ParameterName = "tableName", DbType = System.Data.DbType.String, Value = tableName },
                new System.Data.SqlClient.SqlParameter { ParameterName = "whereStr", DbType = System.Data.DbType.String, Value = whereStr.Replace("\"","'") },
                new System.Data.SqlClient.SqlParameter { ParameterName = "searchColumn", DbType = System.Data.DbType.String, Value = searchColumn },
                new System.Data.SqlClient.SqlParameter { ParameterName = "searchStr", DbType = System.Data.DbType.String, Value = searchStr },
                new System.Data.SqlClient.SqlParameter { ParameterName = "pageIndex", DbType = System.Data.DbType.Int32, Value = pageIndex },
                new System.Data.SqlClient.SqlParameter { ParameterName = "pageSize", DbType = System.Data.DbType.Int32, Value = pageSize },
                new System.Data.SqlClient.SqlParameter { ParameterName = "orderColumn", DbType = System.Data.DbType.String, Value = orderColumn },
                new System.Data.SqlClient.SqlParameter { ParameterName = "order", DbType = System.Data.DbType.String, Value = order }
            };
            

            System.Data.Common.DbCommand command = this.CreateStoreCommand("P_SearchTable", System.Data.CommandType.StoredProcedure, sqlparams);
            

            using (command.Connection.CreateConnectionScope())
            using (System.Data.Common.DbDataReader reader = command.ExecuteReader())
            {
                list = listMaterializer.Materialize(reader).ToList();

                if (reader.NextResult())
                {
                   count = countMaterializer.Materialize(reader).Sum(item => item.count);  
                }
            }

            return list;
        }
        /// <summary>
        /// 通用查询时辅助类
        /// </summary>
        class countClass { public int count { get; set; } }
    
        #endregion

        #region 境外汇款申请书
        /// <summary>
        /// 境外汇款申请书
        /// </summary>
        public ObjectSet<PrintDoc_Bank_OverseasRemittance> PrintDoc_Bank_OverseasRemittance
        {
            get
            {
                if ((_PrintDoc_Bank_OverseasRemittance == null))
                {
                    _PrintDoc_Bank_OverseasRemittance = base.CreateObjectSet<PrintDoc_Bank_OverseasRemittance>("PrintDoc_Bank_OverseasRemittance");
                }
                return _PrintDoc_Bank_OverseasRemittance;
            }
        }
        private ObjectSet<PrintDoc_Bank_OverseasRemittance> _PrintDoc_Bank_OverseasRemittance;
        #endregion

        #region 综合行政-外来信函管理
        #region 快递公司
        /// <summary>
        /// 快递公司
        /// </summary>
        public ObjectSet<Integrated_Letter_Company> Integrated_Letter_Company
        {
            get
            {
                if ((_Integrated_Letter_Company == null))
                {
                    _Integrated_Letter_Company = base.CreateObjectSet<Integrated_Letter_Company>("Integrated_Letter_Company");
                }
                return _Integrated_Letter_Company;
            }
        }
        private ObjectSet<Integrated_Letter_Company> _Integrated_Letter_Company;
        #endregion


        #endregion

        #region 综合管理
        #region 用户管理类
        /// <summary>
        /// 用户管理类
        /// </summary>
        public ObjectSet<ManagerOA_User_User> ManagerOA_User_User
        {
            get
            {
                if ((_ManagerOA_User_User == null))
                {
                    _ManagerOA_User_User = base.CreateObjectSet<ManagerOA_User_User>("ManagerOA_User_User");
                }
                return _ManagerOA_User_User;
            }
        }
        private ObjectSet<ManagerOA_User_User> _ManagerOA_User_User;
        #endregion

       
        #endregion
    }
}
